package com.jinbaoty.recyclebins.utils;

import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.x;

import java.io.File;

public class XUtilDb {

    private static XUtilDb mInstance;

    private DbManager dbManager;

    private XUtilDb() {
    }

    public static XUtilDb get() {
        if (mInstance == null) {
            synchronized (XUtilDb.class) {
                if (mInstance == null) {
                    mInstance = new XUtilDb();
                }
            }
        }
        return mInstance;
    }

    public DbManager getDbManager() {
        if (dbManager == null) {
            initDbManager();
        }
        return dbManager;
    }

    private void initDbManager() {
        File dbFile = new File("/sdcard/recycleBins/db");
        if (!dbFile.exists()) {
            dbFile.mkdirs();
        }
        DbManager.DaoConfig config = new DbManager.DaoConfig()
                .setDbDir(dbFile) //数据库路径
                .setDbName("recycle_bins") //数据库名
                .setDbVersion(1) //设置数据库版本
                .setDbUpgradeListener(new DbManager.DbUpgradeListener() {
                    @Override
                    public void onUpgrade(DbManager db, int oldVersion, int newVersion) {
                        // TODO: 2016/11/8 数据库更新操作
//                        try {
                        //                            db.addColumn(UserInfo.class, "test");
                        //                        } catch (DbException e) {
                        //                            e.printStackTrace();
                        //                        }
                    }
                });
        try {
            dbManager = x.getDb(config);
        } catch (DbException e) {
            e.printStackTrace();
        }
    }
}
